// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.nio.ByteBuffer;
import zombie.characters.IsoPlayer;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.network.PacketValidator;
import zombie.network.packets.INetworkPacket;
import zombie.vehicles.BaseVehicle;

public class VehicleHitPlayerPacket extends VehicleHitPacket implements INetworkPacket {
    protected final Player target = new Player();
    protected final VehicleHit vehicleHit = new VehicleHit();
    protected final Fall fall = new Fall();

    public VehicleHitPlayerPacket() {
        super(HitCharacterPacket.HitType.VehicleHitPlayer);
    }

    public void set(
        IsoPlayer wielder,
        IsoPlayer _target,
        BaseVehicle vehicle,
        float damage,
        boolean isTargetHitFromBehind,
        int vehicleDamage,
        float vehicleSpeed,
        boolean isVehicleHitFromBehind
    ) {
        super.set(wielder, vehicle, false);
        this.target.set(_target, false);
        this.vehicleHit
            .set(
                false,
                damage,
                _target.getHitForce(),
                _target.getHitDir().x,
                _target.getHitDir().y,
                vehicleDamage,
                vehicleSpeed,
                isVehicleHitFromBehind,
                isTargetHitFromBehind
            );
        this.fall.set(_target.getHitReactionNetworkAI());
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        super.parse(b, connection);
        this.target.parse(b, connection);
        this.target.parsePlayer(connection);
        this.vehicleHit.parse(b, connection);
        this.fall.parse(b, connection);
    }

    @Override
    public void write(ByteBufferWriter b) {
        super.write(b);
        this.target.write(b);
        this.vehicleHit.write(b);
        this.fall.write(b);
    }

    @Override
    public boolean isConsistent() {
        return super.isConsistent() && this.target.isConsistent() && this.vehicleHit.isConsistent();
    }

    @Override
    public String getDescription() {
        return super.getDescription()
            + "\n\tTarget "
            + this.target.getDescription()
            + "\n\tVehicleHit "
            + this.vehicleHit.getDescription()
            + "\n\tFall "
            + this.fall.getDescription();
    }

    @Override
    protected void preProcess() {
        super.preProcess();
        this.target.process();
    }

    @Override
    protected void process() {
        this.vehicleHit.process(this.wielder.getCharacter(), this.target.getCharacter(), this.vehicle.getVehicle());
        this.fall.process(this.target.getCharacter());
    }

    @Override
    protected void postProcess() {
        super.postProcess();
        this.target.process();
    }

    @Override
    protected void react() {
        this.target.react();
    }

    @Override
    protected void postpone() {
        this.target.getCharacter().getNetworkCharacterAI().setVehicleHit(this);
    }

    @Override
    public boolean validate(UdpConnection connection) {
        if (!PacketValidator.checkPVP(connection, this.wielder, this.target, VehicleHitPlayerPacket.class.getSimpleName())) {
            return false;
        } else if (!PacketValidator.checkSpeed(connection, this.vehicleHit, VehicleHitPlayerPacket.class.getSimpleName())) {
            return false;
        } else {
            return !PacketValidator.checkShortDistance(connection, this.wielder, this.target, VehicleHitPlayerPacket.class.getSimpleName())
                ? false
                : PacketValidator.checkDamage(connection, this.vehicleHit, VehicleHitPlayerPacket.class.getSimpleName());
        }
    }
}
